package remove_duplicates

import "cmp"

// 双指针
func RemoveDuplicates[T cmp.Ordered](s []T) {
	var (
		i = 0
		j = i + 1
	)

	for j < len(s) {
		if s[i] == s[j] {
			copy(s[j:], s[j+1:])
			s = s[:len(s)-1]
		} else {
			i++
			j++
		}
	}
}

// 快慢指针
func RemoveDuplicates2[T cmp.Ordered](s []T) int {
	n := len(s)
	if n == 0 {
		return 0
	}
	slow := 1
	for fast := 1; fast < n; fast++ {
		if s[fast] != s[fast-1] {
			s[slow] = s[fast]
			slow++
		}
	}
	s = s[:slow]
	return slow
}
